package com.czollmann.android.views;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czollmann.android.CallObject;
import com.czollmann.android.Constants;
import com.czollmann.android.ImageObject;
import com.czollmann.android.ObjectWrapper;
import com.czollmann.android.R;
import com.czollmann.android.SMSObject;
import com.czollmann.android.utils.LogUtils;

public class ListLayout extends LinearLayout {
  public ListLayout(Context context, List<ObjectWrapper> objectWrappers) {
    super(context);
    setOrientation(LinearLayout.VERTICAL);
    for (ObjectWrapper objectWrapper : objectWrappers) {
      LinearLayout layout = (LinearLayout) View.inflate(getContext(),
                                                        R.layout.part_listitem,
                                                        null);
      layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                                              LayoutParams.WRAP_CONTENT));
      /* get views */
      TextView date = (TextView) layout.findViewById(R.id.listitem_date);
      TextView title = (TextView) layout.findViewById(R.id.listitem_title);
      TextView text = (TextView) layout.findViewById(R.id.listitem_text);
      ImageView image = (ImageView) layout.findViewById(R.id.listitem_image);
      LinearLayout contentContainer = (LinearLayout) layout.findViewById(R.id.listitem_contentContainer);
      /* set information */
      date.setText(Constants.timeFormat.print(objectWrapper.getDate()));
      if (objectWrapper.getSmsObject() != null) {
        SMSObject smsObject = objectWrapper.getSmsObject();
        text.setText(smsObject.getMessage());
        if (smsObject.getType() == 1) {
          title.setText(context.getString(R.string.display_sms_from)
                    + smsObject.getNumber());
          image.setImageResource(R.drawable.sms_incoming);
        } else if (smsObject.getType() == 2) {
          title.setText(context.getString(R.string.display_sms_to) + smsObject.getNumber());
          image.setImageResource(R.drawable.sms_outgoing);
        }
      } else if (objectWrapper.getCallObject() != null) {
        CallObject callObject = objectWrapper.getCallObject();
        String calName = context.getString(R.string.unknown);
        String callNameString = "";
        if (callObject.getName() != null) {
          calName = callObject.getName();
        }
        if (callObject.getCount() == 1) {
          callNameString = calName;
        } else {
          callNameString += calName + " (" + callObject.getCount() + ")";
        }
        title.setText(callNameString);
        text.setText(callObject.getNumber());
        if (callObject.getType() == 2) {
          image.setImageResource(R.drawable.call_outgoing);
        } else if (callObject.getType() == 3) {
          image.setImageResource(R.drawable.call_incoming);
        }
      } else if (objectWrapper.getImageObject() != null) {
        ImageObject imageObject = objectWrapper.getImageObject();
        // text.setText();
        title.setText(context.getString(R.string.display_picture_filename)
                  + imageObject.getName());
        image.setImageResource(R.drawable.picture);
        text.setVisibility(INVISIBLE);
        if (imageObject.getPath() != null) {
          // LogUtils.debuglog("adding image object");
          // ImageView imageView = new ImageView(contentContainer.getContext());
          // imageView.setImageBitmap(BitmapFactory.decodeFile(imageObject.getPath()));
          // contentContainer.addView(imageView);
        }
      }
      addView(layout);
    }
  }
}
